package com.example.bailin.giftsay.hot;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseFragment;
import com.example.bailin.giftsay.entity.HotEntity;
import com.example.bailin.giftsay.hot.activity.HotNextActivity;
import com.example.bailin.giftsay.hot.adapter.HotAdapter;
import com.example.bailin.giftsay.main.SearchActivity;
import com.example.bailin.giftsay.sort.LmClickListener;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;
import com.example.bailin.giftsay.tool.UrlProvide;

/**
 * Created by bailin on 16/8/15.
 * 热门
 */
public class HotFragment extends BaseFragment implements View.OnClickListener {

    private RecyclerView rv;
    private HotAdapter adapter;
    private ImageView ivSearch;

    @Override
    protected int setLayout() {
        return R.layout.fragment_hot;
    }

    @Override
    protected void initView() {
        rv = bindView(R.id.rv_hot_id);
        ivSearch = bindView(R.id.iv_hot_search);
        ivSearch.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        tool().getData(UrlProvide.HOT_SPOT, HotEntity.class, new NetTool.NetInterface<HotEntity>() {
            @Override
            public void onSuccess(HotEntity hotEntity) {
                adapter = new HotAdapter();
                adapter.setHotEntity(hotEntity);
                adapter.setcListener(new ShouCListener() {
                    @Override
                    public void onClick(HotEntity entity, int position) {
                        Intent intent = new Intent(MyApp.getContext(), HotNextActivity.class);
                        intent.putExtra("webUrl", entity.getData().getItems().get(position).getData().getUrl());
                        intent.putExtra("url", entity.getData().getItems().get(position).getData().getCover_image_url());
                        intent.putExtra("price", entity.getData().getItems().get(position).getData().getPrice());
                        intent.putExtra("counts", entity.getData().getItems().get(position).getData().getFavorites_count());
                        intent.putExtra("name", entity.getData().getItems().get(position).getData().getName());
                        intent.putExtra("id", entity.getData().getItems().get(position).getData().getId());
                        startActivity(intent);

                    }
                });
                GridLayoutManager manager = new GridLayoutManager(MyApp.getContext(), 2);
                rv.setLayoutManager(manager);
                rv.setAdapter(adapter);
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_hot_search:
                Intent intent = new Intent(MyApp.getContext(), SearchActivity.class);
                startActivity(intent);
                break;
        }
    }
}
